
package com.ht.action;

import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.ht.model.User;
import com.ht.service.UserService;
import com.ht.util.Constants;
import com.ht.util.MyPage;


public class ManageAction  extends ActionSupport {
	
	User user ;
	UserService userService;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/*
	public String login(){
		System.out.println("userActionTest");
		HttpServletRequest request  = ServletActionContext.getRequest();
		request.getSession().setAttribute("username", user.getUsername());
		
		String menuStr = " :null,"
				
                +"寻找倒霉蛋:null;选取成员:personAction!select.action;历史记录:personAction!select.action,"
                +"系统管理员:null;账号管理:userAction!select.action;成员管理:empAction!select.action,"
                +" :null";
                
                
	    request.getSession().setAttribute("menuStr", menuStr);
	  	return "main";	
	}
	*/

	public String doAdd() throws  Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int flag = userService.addUser(user);
		PrintWriter out = response.getWriter();
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to add......");
			target = "failure";
		} else if (flag == 1) {
			request.setAttribute("info", "Add items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.MANA_ACTION);
			target="success";
		} else {
			request.setAttribute("info", "Already exist......");
			target = "failure";
		}
		return target;
	}
	
	public String delete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int flag = userService.deleteUser(user);
		PrintWriter out = response.getWriter();
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to delete......");
			target = "failure";
		} else {
			request.setAttribute("info", "Delete items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.MANA_ACTION);
			target="success";
		}
		return target;
	}

	
	public String toUpdate()throws Exception  {
		User sa = userService.getUserById(user);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("user", sa);
		return "edit";
	}
	

	public String doUpdate() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		int flag = userService.updateUser(user);
		
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to update......");
			target = "failure";
		} else {
			request.setAttribute("info", "Update items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.MANA_ACTION);
			target="success";
		}
		return target;
	}

	public String select() throws Exception {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		String username = request.getParameter("username");
		User user = new User();
		int userId = 0;
		try { 
			userId = Integer.parseInt(id);
			} catch (Exception e) { 
				userId = 999999;
			} 

		if (id != null && !"".equals(id)) {
			user.setId(userId);
		}
		if (username != null && !"".equals(username)) {
			user.setUsername(username);
		}

		String tempPage = request.getParameter("curPage");
		int curPage = 1;
		if(tempPage != null){
			curPage = new Integer(tempPage);
		}
		MyPage mypage = userService.selectByCon(user, curPage);
		request.setAttribute("mypage", mypage);
		return "index";
		

	}
	
	public String check()throws Exception  {
		User sa = userService.getUserById(user);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("user", sa);
		return "check";
	}


	public String getBasePath(HttpServletRequest request) {
		String basePath = request.getScheme() + "://" + request.getServerName()+ ":" + request.getServerPort() + request.getContextPath();
		return basePath;
	}

}
